#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   chacl.sh
# Version:    1.0
# Date:       2013/12/02
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    03系统安全功能-05ACL-02chacl -01修改文件ACL属性
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/02
#             - chacl
# ----------------------------------------------------------------------
##! @TODO: chacl
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoSetup()
{
    # check user is root
    IsRoot
    EchoResult "check user is root"

    trap DoCleanup INT EXIT

    CmdCheck setfacl getfacl chacl expect

    TEST_USER=testu1
    TEST_USER_PASSWORD=setfacl123

    TMP_FILE=/tmp/setfacl$$

    touch ${TMP_FILE}

    # add user
    AddUserPasswd ${TEST_USER} ${TEST_USER_PASSWORD}
    EchoResult "add user ${TEST_USER}"

    # echo > TMP_FILE
    echo "test" > ${TMP_FILE}

}

##! @TODO: change acl of file, and check
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoTest()
{

    # chacl
    chacl u::rw-,g::r--,o::---,u:${TEST_USER}:r--,m::rw- ${TMP_FILE} 
    EchoResult "chacl"

    # getfacl
    getfacl -p ${TMP_FILE} | grep -E "user::rw-|user:${TEST_USER}:r--|group::r--|mask::rw-|other::---"
    EchoResult "getfacl"

    # delect all acls
    chacl -B ${TMP_FILE}
    EchoResult "clear acl"

    # ls -l
    ls -l ${TMP_FILE} | grep -v '+'
    EchoResult "ls -l"
    

}


##! @TODO: cleanup 
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoCleanup()
{
    id -u ${TEST_USER} > /dev/null 2>&1 && userdel -r ${TEST_USER}
    EchoResult "userdel ${TEST_USER}"
    [ -f ${TMP_FILE} ] && rm -f ${TMP_FILE}
}

# include lib files

if [ -z "$SFROOT" ]
then
    echo "SFROOT is null, pls check"
    exit 1
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/Check.sh
. ${SFROOT}/testcases/Security/lib/UserOps.sh

export LANG=C

DoSetup
DoTest
EchoResult "testcase: chacl"
echo ""
